
Imports System.IO
Imports std

Public Module rowmap

    Public Sub row2map(ByRef reader As TextReader, ByRef writer As TextWriter)
        Dim line As String = Nothing
        Dim words As vector(Of String) = Nothing
        line = reader.ReadLine
        While Not line Is Nothing
            strsepar(line, character.tab, words)
            If words.size > 1 Then
                Dim i As Int32
                For i = 1 To words.size() - 1
                    writer.WriteLine(words(0) + character.tab + words(i))
                Next
            End If
            line = reader.ReadLine
        End While
    End Sub

    Public Sub map2row(ByRef reader As TextReader, ByRef writer As TextWriter)
        Dim line As String = Nothing
        Dim lastword As String = Nothing
        Dim words As vector(Of String) = Nothing
        Dim firstline As Boolean = True
        line = reader.ReadLine
        While Not line Is Nothing
            strsepar(line, character.tab, words)
            If words.size() = 2 Then
                If strsame(words(0), lastword, False) Then
                    writer.Write(character.tab + words(1))
                Else
                    If Not firstline Then
                        writer.WriteLine()
                    Else
                        firstline = False
                    End If
                    writer.Write(words(0) + character.tab + words(1))
                    copy(Of String)(lastword, words(0))
                End If
            End If
            line = reader.ReadLine
        End While
    End Sub

    Public Sub Main(ByVal args() As String)
        On Error Resume Next

        If args.Length() < 3 Then
            raiseError("argument count error, rowmap map2row|row2map|2row|2map|map2|row2 input output," _
                        + " if input or output is /-, use console.", errorHandle.errorType.critical)
        Else
            Dim reader As TextReader = Nothing
            Dim writer As TextWriter = Nothing
            If strsame(args(1), "/-", False) Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(1), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            If strsame(args(2), "/-", False) Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(2), FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not reader Is Nothing, "file no found, " + args(0))
            assert(Not writer Is Nothing, "file no found, " + args(1))

            If strsame(args(0), "map2row", False) OrElse _
                strsame(args(0), "2row", False) OrElse _
                strsame(args(0), "map2", False) Then
                map2row(reader, writer)
            ElseIf strsame(args(0), "row2map", False) OrElse _
                strsame(args(0), "2map", False) OrElse _
                strsame(args(0), "row2", False) Then
                row2map(reader, writer)
            Else
                raiseError("first argument should be map2row|row2map|2row|2map|map2|row2." _
                            , errorHandle.errorType.critical)
            End If

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
